﻿package 
{
	import adobe.utils.CustomActions;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;	
	import flash.display.SimpleButton;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;

	/**
	 * ...
	 * @author Antero Juutinen
	 */
	 
	public class Main extends MovieClip	
	{
		// Luokkamuuttujat		
		private var turns:int;
		private var companyName:String;
		private var companyType:String;
		private var money:Number;
		private var employees:int;
		private var maxTurns:int;
		private var wages:int;
		private var loan:int;
		private var income:int;
		private var goodPoints:int;
		private var badPoints:int;
		private var expenses:int;
		private var morale:int;
		private var totalMoney:int;
		private var totalExpenses:int;
		private var tt:ToolTip;
		private var tf:TextFormat
		
		private var bgm:Sound;
		private var bgm2:Sound;
		private var channel:SoundChannel;

		
		private var currentScreen:MovieClip;
		private var currentCtn:MovieClip;
		private var configScreen:ConfigScreen;
		private var menuScreen:MenuScreen;
		private var gameScreen:GameScreen;
		private var gameScreen2:GameScreen2;
		private var socialCtn:SocialContainer;
		private var investmentsCtn:InvestmentsContainer;
		private var ecologicalCtn:EcologicalContainer;
		private var endingScreen:EndingScreen;
		private var stageRef:Stage;
		
				
		public function Main():void
		{	
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
			
		private function init(e:Event = null):void
		{
			// entry point;
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stageRef = stage;
			
			bgm = new Sound(new URLRequest("Audio/lol.mp3"));
			bgm2 = new Sound((new URLRequest("Audio/somewhere_sunny.mp3")));
			channel = bgm.play();
			channel.addEventListener(Event.SOUND_COMPLETE, ChangeMusic);
			
			tt = new ToolTip();
			tf = new TextFormat();
			
			currentScreen = new MovieClip();
			menuScreen = new MenuScreen();
				menuScreen.startBtn.addEventListener(MouseEvent.CLICK, mClickBtn);
			configScreen = new ConfigScreen();
				configScreen.companyTextField.addEventListener(MouseEvent.MOUSE_OVER, toolTipMouseOver);
				configScreen.startBtn.addEventListener(MouseEvent.CLICK, mClickBtn);
			gameScreen = new GameScreen();
				gameScreen.company.buttonMode = true;
				gameScreen.company.addEventListener(MouseEvent.CLICK, mClickBtn);
				gameScreen.nextTurnBtn.addEventListener(MouseEvent.CLICK, NextTurn);
			gameScreen2 = new GameScreen2();
				investmentsCtn = new InvestmentsContainer(); investmentsCtn.x = 63; investmentsCtn.y = 175;
				socialCtn = new SocialContainer(); 			 socialCtn.x = 63; socialCtn.y = 175;
				ecologicalCtn = new EcologicalContainer();	 ecologicalCtn.x = 63; ecologicalCtn.y = 175;
				currentCtn = new MovieClip();
				gameScreen2.investmentsBtn.addEventListener(MouseEvent.CLICK, ChangeContainer);
				gameScreen2.socialBtn.addEventListener(MouseEvent.CLICK, ChangeContainer);
				gameScreen2.ecologicalBtn.addEventListener(MouseEvent.CLICK, ChangeContainer);
				gameScreen2.returnBtn.addEventListener(MouseEvent.CLICK, mClickBtn);
				currentCtn = investmentsCtn;
				gameScreen2.addChild(currentCtn);
			endingScreen = new EndingScreen();			
			
			currentScreen = menuScreen;
			stageRef.addChild(menuScreen);
			trace("INITIADED. START!");	
		}
		
		private function mClickBtn(e:MouseEvent):void 
		{
			switch (currentScreen)
			{
				case menuScreen:
					ChangeScreen(configScreen);
					break;
				case configScreen:
						if (configScreen.gameLengthShort.selected == true)
							maxTurns = 24;
						else if (configScreen.gameLengthMedium.selected == true)
							maxTurns = 44;
						else
							maxTurns = 60;
					money = 10000;
					morale = 10;
					employees = 5;
					gameScreen.moneyTxt.text = money + "€";
					ChangeScreen(gameScreen);
					break;
				case gameScreen:
					ChangeScreen(gameScreen2);
					break;
				case gameScreen2:
					ChangeScreen(gameScreen);
					break;
			}
		}	
		
		private function ChangeScreen(newScreen:MovieClip):void
		{
			if(currentScreen != null)
			{
				stageRef.removeChild(currentScreen);
			}
			currentScreen = newScreen;
			stageRef.addChild(currentScreen);
			var myTweenAlpha:Tween = new Tween(currentScreen, "alpha", Strong.easeOut, 0, 1, 1, true);
			trace(currentScreen);
		}

		private function ChangeMusic(e:Event):void
		{
			trace("Here we are");
			channel = bgm2.play();
		}
		
		private function toolTipMouseOver(e:MouseEvent)
		{
			tf.bold = false;
			tf.size = 12;
			tf.color = 0x000000;
			
			tt.hook = false;
			tt.cornerRadius = 10;
			tt.delay = 500;
			tt.tipWidth = 200;
			tt.tipHeight = 50;
			tt.align = "right";
			tt.titleFormat = tf;
			tt.autoSize = true;
			tt.show( configScreen.companyTextField, "Insert company name", "e.g. Habbo-hotel" );		
		}
		
		private function ChangeContainer(e:MouseEvent):void
		{
			if (e.target.name == "investmentsBtn")
			{
				currentCtn = investmentsCtn;
			}
			else if (e.target.name == "socialBtn")
			{
				currentCtn = socialCtn;
			}
			else
			{
				currentCtn = ecologicalCtn;
			}
				gameScreen2.addChild(currentCtn);
				var myTweenAlpha:Tween = new Tween(currentCtn, "alpha", Strong.easeOut, 0, 1, 1, true);
		}
		
		private function NextTurn(e:MouseEvent):void 
		{
			turns += 1;
			if (turns > maxTurns)
			ChangeScreen(endingScreen);
			trace(turns);
		}	
	}
}